var app = getApp()
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js')
var qqmapsdk
var config = require('../../utils/config')

var status = false;

// 设备信息
var device = {
  width: 375, //设备可用宽度，单位px
  height: 603, //设备可用高度，单位px
  px2rpx: 2, //设备px转rpx值
  rpx2px: 0.5, //设备rpx转px值
  rwidth: 750, //设备可用宽度，单位rpx，规定750rpx
  rheight: 1206, //设备可用高度，单位rpx
  maprHeight: 1206, //地图可用高度，单位rpx
  mapHeight: 603 //地图可用高度，单位px
}
  
// var mapCtx
Page({
  data: {
    animationData: "", 
    status: status,
    map: false,
    userInfo: {},
    position_lng: 0,
    position_lat: 0,
    device,
    inputContent: '',
    markers: [],
    // markers: [{
    //   id: "1",
    //   latitude: 31.305317,
    //   longitude: 121.50027,
    //   width: 50,
    //   height: 50,
    //   iconPath: "/images/icon_people.png",
    //   title: "哪里"
    // }],
    target_user_type:"",
    target_user_error: false,
    target_user_hint: "",
    target_user_info: {},
    current_user_type: app.globalData.userType,
  },
  bindToAgentRegister: function () {
    console.log(app.globalData.userType)
    if (app.globalData.userType == null) {
      console.log("欢迎注册")
      app.globalData.userType = 20
      wx.redirectTo({
        url: '../register/register'
      })
    }
      else{
      console.log("已经成功注册" + app.globalData.userType)
    }
  },
  bindToUserRegister: function () {
    console.log(app.globalData.userType)
    if (app.globalData.userType==null){
      console.log("欢迎注册")
      app.globalData.userType = 10
      wx.redirectTo({
        url: '../register/register'
      })
    }
    else{
      console.log("已经成功注册" + app.globalData.userType)
    }
  },
  bindToLocal: function () {
    wx.redirectTo({
      url: '../local/local'
    })
  },  
  onLoad: function () {
    var that = this

    //得到用户设备信息
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res);
        that.data.device.width = res.windowWidth
        that.data.device.height = res.windowHeight
        that.data.device.px2rpx = 750 / res.windowWidth
        that.data.device.rpx2px = res.windowWidth / 750
        that.data.device.rheight = res.windowHeight * that.data.device.px2rpx
        that.data.device.maprHeight = that.data.device.rheight
        that.data.device.mapHeight = that.data.device.height 

        that.setData({
          device: that.data.device,
        })
        console.log(that.data.device);
      }
    })

    //得到用户定位
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          position_lng: longitude,
          position_lat: latitude,
          map: true
        })
        that.getLocalInfo()
      }
    })



    qqmapsdk = new QQMapWX({
      key: 'YSEBZ-RLCHU-MCPVC-4YYWU-WQN53-IJFGQ'
      //key: 'UAUBZ-W26HP-LM3DZ-LXWQ3-SDJY2-4TFDG'
    })

  },
  getLocalInfo: function () {
    let that = this
    //调用request请求api转换登录凭证  
    var user_type = wx.getStorageSync('user_type')
    console.log(user_type)
    console.log(config.config.localInfoUrl +
      '?current_user_type=' + user_type +
      '&location_lng=' + that.data.position_lng +
      '&location_lat=' + that.data.position_lat +
      '&scale_ranger=16')
    wx.request({
      url: config.config.localInfoUrl + 
            '?current_user_type=' + user_type + 
            '&location_lng=' + that.data.position_lng + 
            '&location_lat=' + that.data.position_lat + 
            '&scale_ranger=16',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (!res.data.result) {
          console.log("getLocalInfo fail case 1")
          console.log(res)
        } else {
          console.log("getLocalInfo success")
          that.setData({
            markers: res.data.data
          })
          console.log(res.data.data)
          console.log(that.data.markers)
        }
      },
      fail: function (res) {
        console.log("getLocalInfo fail case 2")
        console.log(res)
        // wx.setStorageSync('user_type', -1)
      }
    })
  },  
  // 页面渲染完成后，启动信道
  onReady: function () {
    let that = this

    wx.setNavigationBarTitle({ title: '附近的人' })
    this.mapCtx = wx.createMapContext('map')
    // mapCtx = this.mapCtx

    if (!this.pageReady) {
      this.pageReady = true
    }

    qqmapsdk.reverseGeocoder({
      success: function (res) {
        console.log(res.result.location)
        that.setData({
          position_lat:res.result.location.lat,
          position_lng:res.result.location.lng
        })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  regionchange:function(e){
    var that = this 
    console.log("regionchange start")
    if(e.type== "end"){
      this.mapCtx.getCenterLocation({
        success: function (res) {
          console.log(res.latitude)
          console.log(res.longitude)
          that.setData({
            position_lat: res.latitude,
            position_lng: res.longitude
          })
          that.getLocalInfo()
        }
      })
    }
  },
  toastHide:function(e){
    console.log("Map taped");
    this.regionchange(e);
  },
  markertap:function(e){
    let that = this
    console.log("Function markertap step in")
    console.log(e.markerId)
    console.log(app.globalData.userType)
    if (app.globalData.userType == 0 || app.globalData.userType == "0")
    {
      wx.redirectTo({
        url: '../register/register'
      })
    }
    wx.request({
      url: config.config.contactInfoUrl + '?target_user_id=' + e.markerId + '&current_user_type=' + app.globalData.userType,
      // data: user_data,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if(res.data.result){
          console.log(res.data.info)
          that.setData({
            target_user_type: res.data.info.type,
            target_user_info: res.data.info,
            target_user_error: false,
          })
          wx.navigateTo({
            url: '../detail/detail' + 
            '?target_user_type=' + res.data.info.type + 
            '&target_user_info=' + JSON.stringify(res.data.info) + 
            '&target_user_error=' + false
          })
        }else{
          console.log(res.data.hint)
          that.setData({
            target_user_error: true,
            target_user_hint: res.data.hint,
            target_user_info: {},
          })
          wx.navigateTo({
            url: '../detail/detail' +
            '?target_user_hint=' + res.data.hint +
            '&target_user_error=' + true
          })
        }
      },
      fail:function(e){
        console.log(e)
        that.setData({
          target_user_error: true,
          target_user_hint: e,
          target_user_info: {},
        })
        wx.navigateTo({
          url: '../detail/detail' +
          '?target_user_hint=' + e +
          '&target_user_error=' + true
        })
      }
    })
    console.log("Function markertap step out")
  },
})